Let's say you have a Ruby class with a method you'd like to wrap—for debugging or performance timing—and, since you don't control where the class is instantiated (think overriding a method in Rails' ActionPack), just creating a subclass and using super isn't going to work.
Let's take a look at two mixin patterns; one a ubiquitous naming hack and one a bit of esoteric Ruby inheritance trickery.
First, let's set the scene. Let's say we have a method, Widget#render_on:
class Widget
# Wrap this one
def render_on(document, options = {})
# ...
end
# ...
end
What we want is for each wrapper we write to call a method, which we'll call snapshot, that will output some information about the method invocation. Don't worry about the implementation too much here; it just takes a block and times the yield:
def snapshot
start = Time.now
result = yield
# You could store these values somewhere
# for averaging, track the caller, etc
puts Time.now - start
end
I used to be an avid Pythonista. I poured over Python books, read huge swathes of Python code (including the source of Twisted, which, as its name might imply, I think of as quite a personal accomplishment), and soaked up as much material as I could find. I had survived the relative chaos of Perl, and was relieved to find a more orderly, seemingly sane language to work with. I dove in, and I loved it.
I learned a huge amount during those years; Python was my first OOP language, and cemented what has become my obsession with dynamic, “lightweight” languages. I’ve never studied CS formally (having opted for languages and linguistics instead) so Python’s more draconian rules and academic bent helped temper my more artistic tendencies and pushed me far enough into the land of algorithms, data structures, and big O notation so not to be seen as a complete dilettante by later CS nerd colleagues.
One day, I stumbled across a lone copy of Programming Ruby by chance at a bookstore (back when you just didn’t find Ruby books). At the time, I thought of Ruby as merely “a Japanese Perl,” so I only picked it up on a whim — but only a few chapters in, I was completely hooked.
Although I continued to use Python (and PHP) professionally for the next couple of years, and very successfully — I spent my spare time on Ruby, and became very active in the community. In 2004 I fully switched, and became a professional Ruby developer.
I haven’t looked back.
Okay, looking back
Recently I’ve had the need to do a proof-of-concept across multiple “production quality” OO languages, so it seemed only natural to dig back into Python a bit.
I discovered a language that had matured significantly with a smart community — that now included some “younger” blood and a fair bit of excitement.
I had to tell this story several dozen times at RubyConf, so I thought I’d wrap it up for posterity.
It all started with an email. The campaign was running a contest entitled “Front Row to History,” and would select
5 first-time and 5 repeat donators to attend the rally in Chicago— what would later become a massive victory celebration.
I had donated to the campaign before and had, like many of you, become more and more fervent in my support of Obama as election day approached. I’m an independent voter, but my support was won after researching policies and watching the tone of the campaigns as they progressed.
So, I did a bit of early voting with my wallet. Again.
Welcome to the personal website of
Bruce Williams,
longtime Rubyist, graphic designer, language enthusiast, amateur chef, and oil painter of little skill. I am a work in progress.
I have the good fortune to write code for a living. Take a peek at my work and personal projects.